/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.laporananggaranpendapatandanabantuanpemerintah;

import java.util.HashMap;
import java.util.Map;
import net.sf.jasperreports.engine.data.JRTableModelDataSource;
import org.sadhar.sia.common.ClassConnection;
import org.sadhar.sia.common.ClassSession;
import org.sadhar.sia.framework.ClassApplicationModule;
import org.sadhar.sipp.laporananggaranpendapatanuangformulirmahasiswabaru.ResultSetTableModel;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Session;
import org.zkoss.zk.ui.Sessions;
import org.zkoss.zkex.zul.Jasperreport;
import org.zkoss.zul.Button;
import org.zkoss.zul.Label;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Panel;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Window;

/**
 *
 * @author Yohan Hardinugroho
 */
public class LaporanAnggaranPendapatanDanaBantuanPemerintahWnd extends ClassApplicationModule {

    private Session sesi;

    public LaporanAnggaranPendapatanDanaBantuanPemerintahWnd() {
        sesi = Sessions.getCurrent();
        session = (ClassSession) sesi.getAttribute("CURRENT_SESSION");
        banpemDAO = new LaporanAnggaranPendapatanDanaBantuanPemerintahDAOImpl();
    }
    private Label lblTahunAnggaran;
    private Label lblUnitKerja;
    private Button buttonExport;
    private Button buttonHideExport;
    private Jasperreport report;
    private ClassSession session;
    private Panel panelReport;
    private Listbox lstboxformat;
    private Textbox txtboxKeySearch;
    private Button btnCari;
    private Textbox txtboxKd_unit_kerja;
    private LaporanAnggaranPendapatanDanaBantuanPemerintahDAO banpemDAO;

    public void onCreate() throws Exception {
        panelReport = (Panel) getFellow("panelReport");
        lblTahunAnggaran = (Label) getFellow("lblTahunAnggaran");
        lblUnitKerja = (Label) getFellow("lblUnitKerja");
        buttonExport = (Button) getFellow("buttonExport");
        buttonHideExport = (Button) getFellow("buttonHideExport");
        report = (Jasperreport) getFellow("report");
        lstboxformat = (Listbox) getFellow("lstboxformat");

        txtboxKeySearch = (Textbox) getFellow("txtboxKeySearch");
        btnCari = (Button) getFellow("btnCari");
        txtboxKd_unit_kerja = (Textbox) getFellow("txtboxKd_unit_kerja");



        lblTahunAnggaran.setValue(session.getThnAnggaran());

        load();

    }

    public void load() {
    }

    public void export() throws Exception {
        String sql = "";
        report.setType(lstboxformat.getSelectedItem().getValue().toString());
        report.setSrc("reports/laporananggaranpendapatandanabantuanpemerintah/LaporanAnggaranPendapatanDanaBantuanPemerintah.jasper");
        boolean emptyornot = txtboxKeySearch.getValue().trim().isEmpty();
        if (emptyornot) {
            sql = banpemDAO.getQueryBanpemAll(session.getThnAnggaran());
            //Kalau kosong
        } else {
            sql = banpemDAO.getQueryBanpemByUnit(session.getThnAnggaran(), txtboxKd_unit_kerja.getValue());
            //Kalau isi
        }

        System.out.println(sql);

        Map m = new HashMap();
        m.put("tahunAnggaran", session.getThnAnggaran());
        report.setParameters(m);
        ResultSetTableModel rstm = new ResultSetTableModel(ClassConnection.getJdbc().getDataSource().getConnection(), sql);
        report.setDatasource(new JRTableModelDataSource(rstm));
        showExport();
    }

    public void showExport() {
        panelReport.setVisible(true);
        panelReport.setOpen(true);
        buttonHideExport.setVisible(true);
    }

    public void hideExport() {
        panelReport.setVisible(false);
        buttonHideExport.setVisible(false);
    }

    public void btnCariOnClick() throws Exception {
        final Window win = (Window) Executions.createComponents(
                "sippzul/laporananggaranpendapatandanabantuanpemerintah/UnitKerjaPopup.zul", this,
                null);
        Textbox txtboxSearch = (Textbox) win.getFellow("txtboxSearch");
        txtboxSearch.setValue(txtboxKeySearch.getValue());
        win.doModal();

        txtBoxKdUnitKerjaOnChange();
    }

    public void txtBoxKdUnitKerjaOnChange() {
        lblUnitKerja.setValue(lblUnitKerja.getValue());
        if (txtboxKeySearch.getValue().trim().isEmpty()) {
            txtboxKd_unit_kerja.setValue("");
        }
        txtboxKeySearch.setValue("");
    }
}
